﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;

namespace CoreDataTypes.RelatedLinks.RazorModel
{
    public class Links : IEnumerable, IHtmlString
    {
        public Links()
        {
            Items = new List<Link>();
        }
        public string Target
        {
            get
            {
                var first = Items.FirstOrDefault();
                if (first != null)
                {
                    return first.Target;
                }
                return string.Empty;
            }
        }
        public string Title
        {
            get
            {
                var first = Items.FirstOrDefault();
                if (first != null)
                {
                    return first.Title;
                }
                return string.Empty;
            }
        }
        public string Href { get { return Url; } }
        public string Url
        {
            get
            {
                var first = Items.FirstOrDefault();
                if (first != null)
                {
                    return first.Url;
                }
                return string.Empty;
            }
        }

        public int Count()
        {
            return Items.Count;
        }

        public List<Link> Items;

        #region IHtmlString Members

        public string ToHtmlString()
        {
            if (Items.Count == 1)
            {
                return Items.FirstOrDefault().ToHtmlString();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<ul class=\"links\">");
                foreach (var item in Items)
                {
                    sb.Append("<li>");
                    sb.AppendFormat(item.ToHtmlString());
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
                return sb.ToString();
            }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        #endregion
    }
}
